{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 点击只能确定的弹窗"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "23d386f0c862e5ed"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alert Message\n"
     ]
    },
    {
     "ename": "WebDriverException",
     "evalue": "Message: disconnected: not connected to DevTools\n  (failed to check if window was closed: disconnected: not connected to DevTools)\n  (Session info: MicrosoftEdge=126.0.2592.56)\nStacktrace:\n\tGetHandleVerifier [0x00007FF6AE878152+13538]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE801E09+595497]\n\t(No symbol) [0x00007FF6AE61E6CF]\n\t(No symbol) [0x00007FF6AE6089DA]\n\t(No symbol) [0x00007FF6AE60978F]\n\t(No symbol) [0x00007FF6AE61EBC2]\n\t(No symbol) [0x00007FF6AE5FAF15]\n\t(No symbol) [0x00007FF6AE68E091]\n\t(No symbol) [0x00007FF6AE681FC0]\n\t(No symbol) [0x00007FF6AE65766E]\n\t(No symbol) [0x00007FF6AE65683C]\n\t(No symbol) [0x00007FF6AE657221]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF6AEA396F4+1099860]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE77D8FC+53532]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE770E25+1605]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF6AEA38685+1095653]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6AE80C981+27777]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6AE806D04+4100]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6AE806E3B+4411]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE7FCFC0+575456]\n\tBaseThreadInitThunk [0x00007FFD7D93257D+29]\n\tRtlUserThreadStart [0x00007FFD7F08AA48+40]\n",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mWebDriverException\u001B[0m                        Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-6-5d8681f8d1fa>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m     19\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     20\u001B[0m \u001B[0mtime\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0msleep\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;36m5\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 21\u001B[1;33m \u001B[0mdriver\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mclose\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;32mE:\\Program\\Anaconda\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001B[0m in \u001B[0;36mclose\u001B[1;34m(self)\u001B[0m\n\u001B[0;32m    456\u001B[0m                 \u001B[0mdriver\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mclose\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    457\u001B[0m         \"\"\"\n\u001B[1;32m--> 458\u001B[1;33m         \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mexecute\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mCommand\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mCLOSE\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    459\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    460\u001B[0m     \u001B[1;32mdef\u001B[0m \u001B[0mquit\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;33m->\u001B[0m \u001B[1;32mNone\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mE:\\Program\\Anaconda\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001B[0m in \u001B[0;36mexecute\u001B[1;34m(self, driver_command, params)\u001B[0m\n\u001B[0;32m    345\u001B[0m         \u001B[0mresponse\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mcommand_executor\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mexecute\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdriver_command\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mparams\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    346\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0mresponse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 347\u001B[1;33m             \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0merror_handler\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mcheck_response\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mresponse\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    348\u001B[0m             \u001B[0mresponse\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m\"value\"\u001B[0m\u001B[1;33m]\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_unwrap_value\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mresponse\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mget\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"value\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;32mNone\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    349\u001B[0m             \u001B[1;32mreturn\u001B[0m \u001B[0mresponse\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mE:\\Program\\Anaconda\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001B[0m in \u001B[0;36mcheck_response\u001B[1;34m(self, response)\u001B[0m\n\u001B[0;32m    227\u001B[0m                 \u001B[0malert_text\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mvalue\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m\"alert\"\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mget\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"text\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    228\u001B[0m             \u001B[1;32mraise\u001B[0m \u001B[0mexception_class\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mmessage\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mscreen\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mstacktrace\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0malert_text\u001B[0m\u001B[1;33m)\u001B[0m  \u001B[1;31m# type: ignore[call-arg]  # mypy is not smart enough here\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 229\u001B[1;33m         \u001B[1;32mraise\u001B[0m \u001B[0mexception_class\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mmessage\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mscreen\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mstacktrace\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mWebDriverException\u001B[0m: Message: disconnected: not connected to DevTools\n  (failed to check if window was closed: disconnected: not connected to DevTools)\n  (Session info: MicrosoftEdge=126.0.2592.56)\nStacktrace:\n\tGetHandleVerifier [0x00007FF6AE878152+13538]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE801E09+595497]\n\t(No symbol) [0x00007FF6AE61E6CF]\n\t(No symbol) [0x00007FF6AE6089DA]\n\t(No symbol) [0x00007FF6AE60978F]\n\t(No symbol) [0x00007FF6AE61EBC2]\n\t(No symbol) [0x00007FF6AE5FAF15]\n\t(No symbol) [0x00007FF6AE68E091]\n\t(No symbol) [0x00007FF6AE681FC0]\n\t(No symbol) [0x00007FF6AE65766E]\n\t(No symbol) [0x00007FF6AE65683C]\n\t(No symbol) [0x00007FF6AE657221]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF6AEA396F4+1099860]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE77D8FC+53532]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE770E25+1605]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF6AEA38685+1095653]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6AE80C981+27777]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6AE806D04+4100]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6AE806E3B+4411]\n\tMicrosoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6AE7FCFC0+575456]\n\tBaseThreadInitThunk [0x00007FFD7D93257D+29]\n\tRtlUserThreadStart [0x00007FFD7F08AA48+40]\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.select import Select\n",
    "\n",
    "driver = webdriver.Edge()\n",
    "driver.get(\"https://sahitest.com/demo/alertTest.htm\")\n",
    "\n",
    "driver.maximize_window() # 页面最大化\n",
    "\n",
    "# 定位触发弹框的元素\n",
    "driver.find_element(By.NAME,\"b1\").click()\n",
    "\n",
    "print(driver.switch_to.alert.text)\n",
    "time.sleep(1)\n",
    "# 弹框->点击确定\n",
    "driver.switch_to.alert.accept()\n",
    "\n",
    "time.sleep(5)\n",
    "driver.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-15T05:17:50.788315900Z"
    }
   },
   "id": "initial_id"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 点击可以确定和取消的弹窗"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "58dd4d69c5c5412"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Some question?\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.select import Select\n",
    "\n",
    "driver = webdriver.Edge()\n",
    "driver.get(\"https://sahitest.com/demo/confirmTest.htm\")\n",
    "\n",
    "driver.maximize_window() # 页面最大化\n",
    "\n",
    "# 定位触发弹框的元素\n",
    "driver.find_element(By.NAME,\"b1\").click()\n",
    "\n",
    "print(driver.switch_to.alert.text)\n",
    "time.sleep(1)\n",
    "# 弹框->点击确定\n",
    "\"\"\"driver.switch_to.alert.accept()\"\"\"\n",
    "# 弹框->点击取消\n",
    "driver.switch_to.alert.dismiss()\n",
    "time.sleep(5)\n",
    "driver.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-15T05:20:46.533955300Z",
     "start_time": "2024-06-15T05:20:34.810378500Z"
    }
   },
   "id": "6dc5c408a03bb72c"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 点击可以输入内容的弹窗"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2d56d4d982dc7446"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Some prompt?\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.select import Select\n",
    "\n",
    "driver = webdriver.Edge()\n",
    "driver.get(\"https://sahitest.com/demo/promptTest.htm\")\n",
    "\n",
    "driver.maximize_window() # 页面最大化\n",
    "\n",
    "# 定位触发弹框的元素\n",
    "driver.find_element(By.NAME,\"b1\").click()\n",
    "\n",
    "print(driver.switch_to.alert.text)\n",
    "time.sleep(1)\n",
    "\n",
    "# 往弹窗里面输入内容\n",
    "driver.switch_to.alert.send_keys(\"ooookkkk\")\n",
    "time.sleep(1)\n",
    "# 确定点击弹窗\n",
    "driver.switch_to.alert.accept()\n",
    "\n",
    "\n",
    "time.sleep(5)\n",
    "driver.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-15T05:24:53.728700800Z",
     "start_time": "2024-06-15T05:24:42.896017800Z"
    }
   },
   "id": "3c57aa3d905560a7"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "fc5d6327cc35c697"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "d6ee052c9c3fb3d4"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "a4e156411049b28a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
